<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" href="../rurple.css" type="text/css"/>

<title>Object-Oriented Programming: "dot" notation</title>
</head>
<body>
<h2 class="title">30. Object-Oriented Programming: "dot" notation</h2>

<p>We are soon going to learn about a modern programming style called
<em>Object-Oriented Programming</em> [OOP]. Python is an OOP language, although you could never have guessed from what we have seen up to now.
Before we start writing object-oriented programs, we will
first learn how to read and understand the notation used.</p>

<!-- ======================== -->
<hr class="line" />

<h3 class="section">All in a dog's day</h3>

<p>Fido is a dog. During a typical day, he does various actions: he eats,
runs, sleeps, etc. Here's how an object-oriented programmer might write
this.</p>

<pre>
Fido = Dog()
Fido.eats()
Fido.runs()
Fido.sleeps()
</pre>

<p>In addition, Fido has various qualities or <i>attributes</i>. These are  <i>variables</i>, like we have seen before except that they "belong" to Fido.  He is tall (for a dog) and his hair is black. Here's how the programmer might write the same things.</p>

<pre>
Fido.size = <span class="string">"tall"</span>
Fido.hair_colour = <span class="string">"black"</span>
</pre>

<p>In the object-oriented language, we have the following:</p>

<ul>
<li>
<tt>Dog</tt> is an example of a <i>class</i> (of objects).
</li>
<li>
<tt>Fido</tt> is an <i>instance</i> (or particular object) in the 
<tt>Dog</tt> <i>class</i>.
</li>
<li>
A class can be thought of as a special function which creates instances.
</li>
<li>
<tt>eats(), runs()</tt> and <tt>sleeps()</tt> are <i>methods</i> of the
<tt>Dog</tt> <i>class</i>; 'methods' are essentially like 'functions' which
we saw before (the only difference is that they belong in a given
class/instance).
</li>
<li>
<tt>size</tt> and <tt>hair_colour</tt> are <i>attributes</i> of a given
instance/object; attributes can take any value that a "normal" variable can take.
</li>
<li>
The connection between the attributes or the methods with the object is indicated by a "dot" (".") written between them.
</li>
</ul>

<p>Objects can also have other objects that belong to them, each with their
own methods or attributes:</p>

<pre>
Fido.tail.wags()
Fido.tail.type = <span class="string">"bushy"</span>
Fido.left_front_paw.moves()
Fido.head.mouth.teeth.canine.hurt()
</pre>

<p>We'll see how this works later. For now, let's see how Reeborg can use the
"dot" notation.</p>

<!-- ======================== -->
<hr class="line" />

<h3 class="try">A used robot get his name</h3>

<p>So far, all the programs we wrote instructing Reeborg to accomplish tasks
have been written without using the Object-Oriented Programming (OOP)
notation. Let's start with a simple example.</p>

<p>First, we start by having an empty world, removing the robot if needed by
pressing the add/remove robot button <img alt="add/remove robot" src=
"../../images/inter/btn_add_robot.png" /></p>

<p>Now, you might remember that RUR in RUR-PLE stands for: Roberge's <b><span style="text-decoration: underline;">Used
Robot</span></b>; the robots we use are old and faulty. <small>[We will learn how to
fix them later.]</small> So, we will create our first instance of the
<tt>UsedRobot</tt> class and name it, appropriately, Reeborg! We will then
instruct it to take one step and then turn itself off.</p>

<pre>
Reeborg = UsedRobot()
Reeborg.move()
Reeborg.turn_off()
</pre>

<p><b>Try it!</b></p>

<!-- ======================== -->
<hr class="line" />

<h3 class="try">More robots</h3>

<p>Just like functions can have arguments, methods can too. Start with an
empty world and try the following:</p>

<pre>
<span class=
"comment"># add robot at origin [by default], but with more interesting colour</span>
Larry = UsedRobot(colour=<span class="string">'blue'</span>)
<span class=
"comment"># second robot, default colour (grey) facing North</span>
Curly = UsedRobot(1, 3, <span class="string">'N'</span>)
<span class="comment">#  Third robot carries beepers</span>
Moe = UsedRobot(1, 2, beepers=9, colour=<span class="string">'yellow'</span>)

Larry.move()
Curly.move()
Moe.move()
Larry.move()
Curly.move()
Moe.move()

Curly.turn_left()
Larry.move()
Curly.move()
<span class="comment"># Turning off any one robot ends the program</span>
Moe.turn_off()
</pre>

<p>Robots come in a variety of colours: grey (by default), yellow, blue,
light blue, green and purple. They can be positioned anywhere in the world
(with more than one robot at the same intersection), face any of the four
directions ('E' <small>[by default]</small>, 'N', 'S', 'W') or carry a number
of beepers from the start. Note that there are two <b>named</b> arguments
(beepers and colour) and three <b>unnamed</b> ones (street, avenue and
orientation). The two named arguments must appear last (their order may be
interchanged if we write their name); 
the three unnamed arguments, if they appear, must appear in
the same order. Thus, if we want to specify an orientation (say 'N'), we must
first specify a street and an avenue so that the orientation is the third
argument.</p>

<p>The following are allowed declarations:</p>

<pre>
R1 = UsedRobot(2) <span class=
"comment"># created at 2nd street, 1st avenue, facing East</span>
R2 = UsedRobot(2, 3) <span class=
"comment"># 2nd street, 3rd avenue, facing East</span>
R3 = UsedRobot(3, 1, <span class="string">'S'</span>) <span class=
"comment"># 3rd steet, 1st avenue, facing South</span>
R4 = UsedRobot(5, colour=<span class="string">'yellow'</span>) <span class=
"comment"># 5th street, 1st avenue, facing East</span>
</pre>

<p>The following declarations are <b>not allowed</b>:</p>

<pre>
R5 = UsedRobot(3, <span class="string">'S'</span>) <span class=
"comment"># orientation is <b>not</b> 3rd argument</span>
R6 = UsedRobot(colour=<span class="string">'yellow'</span>, 5) <span class=
"comment"># unnamed argument listed after named one</span>
</pre>

<div class="lessons_nav">
<a href="29-strings.htm"><img alt="previous" src=
"../../images/previous.png" />What's your name?</a> - <a href=
"../lessons_toc.htm"><img alt="home" src="../../images/home.png" /></a> - <a href=
"31-global.htm">Globalization is a bad thing <img alt="next"
src="../../images/next.png" /></a>
</div>
</body>
</html>
